﻿using BCCommon;
using BCCommon.Enums;
using BCDto.OA.WorkflowTemplate.WorkflowTemplate;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplate;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCData.OA.WorkflowTemplate.WorkflowTemplate;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplatePermission;
using BCData.OA.WorkflowTemplate.WorkflowTemplatePermission;
using BCDto.OA.WorkflowTemplate.WorkflowTemplatePermission;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateFlowStepDesign;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Converters;
using System.Globalization;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStep;
using Newtonsoft.Json.Linq;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepTypeConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepBaseConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepStartBaseConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepFormTemplateConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateFlowStepDesign;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepStartBaseConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateFlowStepDesign;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStep;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStep;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepTypeConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepTypeConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepLineConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepLineConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepLineConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepLinePathConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepLinePathConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepLinePathConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepStartBaseConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepFormTemplateConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepFormTemplateConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepBaseConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepBaseConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepReviewerConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepReviewerConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepReviewerConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepButtonConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepButtonConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepButtonConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepNotificationConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepNotificationConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepNotificationConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepCountersignConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepCountersignConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepCountersignConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepConditionConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepConditionConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepConditionConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepEndBaseConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepEndBaseConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepEndBaseConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepConditionBaseConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepConditionBaseConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepConditionBaseConfig;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepSubFlowConfig;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepSubFlowConfig;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepSubFlowConfig;
using BCCommon.OAEnums;
using BCDto.Sim.Admin.Admin;
using BCData.Sim.Company.Position;

namespace BCService.OA.WorkflowTemplate.WorkflowTemplate
{
    public class WorkflowTemplateService : IWorkflowTemplateService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IPositionData positionData;
        private readonly IWorkflowTemplateData workflowTemplateData;
        private readonly IWorkflowTemplatePermissionData workflowTemplatePermissionData;
        private readonly IWorkflowTemplateFlowStepDesignData workflowTemplateFlowStepDesignData;
        private readonly IWorkflowTemplateStepTypeConfigData workflowTemplateStepTypeConfigData;
        private readonly IWorkflowTemplateStepData workflowTemplateStepData;
        private readonly IWorkflowTemplateStepLineConfigData workflowTemplateStepLineConfigData;
        private readonly IWorkflowTemplateStepLinePathConfigData workflowTemplateStepLinePathConfigData;
        private readonly IWorkflowTemplateStepStartBaseConfigData workflowTemplateStepStartBaseConfigData;
        private readonly IWorkflowTemplateStepFormTemplateConfigData workflowTemplateStepFormTemplateConfigData;
        private readonly IWorkflowTemplateStepBaseConfigData workflowTemplateStepBaseConfigData;
        private readonly IWorkflowTemplateStepReviewerConfigData workflowTemplateStepReviewerConfigData;
        private readonly IWorkflowTemplateStepButtonConfigData workflowTemplateStepButtonConfigData;
        private readonly IWorkflowTemplateStepNotificationConfigData workflowTemplateStepNotificationConfigData;
        private readonly IWorkflowTemplateStepCountersignConfigData workflowTemplateStepCountersignConfigData;
        private readonly IWorkflowTemplateStepConditionBaseConfigData workflowTemplateStepConditionBaseConfigData;
        private readonly IWorkflowTemplateStepConditionConfigData workflowTemplateStepConditionConfigData;
        private readonly IWorkflowTemplateStepEndBaseConfigData workflowTemplateStepEndBaseConfigData;
        private readonly IWorkflowTemplateStepSubFlowConfigData workflowTemplateStepSubFlowConfigData;
        private readonly string key = string.Format("{0}", EntityNameConst.WorkflowTemplateEntity);
        private readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
        {
            ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy { ProcessDictionaryKeys = false } },
            Converters = new List<JsonConverter> {
                new StringEnumConverter { AllowIntegerValues = true },
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeLocal, DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }
            },
            NullValueHandling = NullValueHandling.Ignore
        };
        private readonly JsonSerializerSettings ResultDictionaryJsonSerializerSettings = new JsonSerializerSettings
        {
            ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy { ProcessDictionaryKeys = true, OverrideSpecifiedNames = true } },
            Converters = new List<JsonConverter> {
                new StringEnumConverter { AllowIntegerValues = true },
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeLocal, DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }
            },
            NullValueHandling = NullValueHandling.Ignore
        };
        public WorkflowTemplateService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IPositionData positionData,
            IWorkflowTemplateData workflowTemplateData,
            IWorkflowTemplatePermissionData workflowTemplatePermissionData,
            IWorkflowTemplateFlowStepDesignData workflowTemplateFlowStepDesignData,
            IWorkflowTemplateStepTypeConfigData workflowTemplateStepTypeConfigData,
            IWorkflowTemplateStepData workflowTemplateStepData,
            IWorkflowTemplateStepLineConfigData workflowTemplateStepLineConfigData,
            IWorkflowTemplateStepLinePathConfigData workflowTemplateStepLinePathConfigData,
            IWorkflowTemplateStepStartBaseConfigData workflowTemplateStepStartBaseConfigData,
            IWorkflowTemplateStepFormTemplateConfigData workflowTemplateStepFormTemplateConfigData,
            IWorkflowTemplateStepBaseConfigData workflowTemplateStepBaseConfigData,
            IWorkflowTemplateStepReviewerConfigData workflowTemplateStepReviewerConfigData,
            IWorkflowTemplateStepButtonConfigData workflowTemplateStepButtonConfigData,
            IWorkflowTemplateStepNotificationConfigData workflowTemplateStepNotificationConfigData,
            IWorkflowTemplateStepCountersignConfigData workflowTemplateStepCountersignConfigData,
            IWorkflowTemplateStepConditionBaseConfigData workflowTemplateStepConditionBaseConfigData,
            IWorkflowTemplateStepConditionConfigData workflowTemplateStepConditionConfigData,
            IWorkflowTemplateStepEndBaseConfigData workflowTemplateStepEndBaseConfigData,
            IWorkflowTemplateStepSubFlowConfigData workflowTemplateStepSubFlowConfigData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.positionData = positionData;
            this.workflowTemplateData = workflowTemplateData;
            this.workflowTemplatePermissionData = workflowTemplatePermissionData;
            this.workflowTemplateFlowStepDesignData = workflowTemplateFlowStepDesignData;
            this.workflowTemplateStepTypeConfigData = workflowTemplateStepTypeConfigData;
            this.workflowTemplateStepData = workflowTemplateStepData;
            this.workflowTemplateStepLineConfigData = workflowTemplateStepLineConfigData;
            this.workflowTemplateStepLinePathConfigData = workflowTemplateStepLinePathConfigData;
            this.workflowTemplateStepStartBaseConfigData = workflowTemplateStepStartBaseConfigData;
            this.workflowTemplateStepFormTemplateConfigData = workflowTemplateStepFormTemplateConfigData;
            this.workflowTemplateStepBaseConfigData = workflowTemplateStepBaseConfigData;
            this.workflowTemplateStepReviewerConfigData = workflowTemplateStepReviewerConfigData;
            this.workflowTemplateStepButtonConfigData = workflowTemplateStepButtonConfigData;
            this.workflowTemplateStepNotificationConfigData = workflowTemplateStepNotificationConfigData;
            this.workflowTemplateStepCountersignConfigData = workflowTemplateStepCountersignConfigData;
            this.workflowTemplateStepConditionBaseConfigData = workflowTemplateStepConditionBaseConfigData;
            this.workflowTemplateStepConditionConfigData = workflowTemplateStepConditionConfigData;
            this.workflowTemplateStepEndBaseConfigData = workflowTemplateStepEndBaseConfigData;
            this.workflowTemplateStepSubFlowConfigData = workflowTemplateStepSubFlowConfigData;
        }

        #region 检查工作流模板是否存在
        public bool Exists(long workflowTemplateId)
        {
            return this.workflowTemplateData.Exists(workflowTemplateId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加工作流模板
        public WorkflowTemplateDto Add(WorkflowTemplateRequestDto requestDto,AdminDto adminDto)
        {
            var entity = requestDto.As<WorkflowTemplateEntity>();
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;

            List<WorkflowTemplatePermissionEntity> permissionConfigEntities = requestDto.PermissionConfig.As<List<WorkflowTemplatePermissionEntity>>();
            databaseContext.BeginTransaction();

            var result = this.workflowTemplateData.Add(entity).GetAwaiter().GetResult().As<WorkflowTemplateDto>();
            if (permissionConfigEntities != null && permissionConfigEntities.Any())
            {
                foreach (var item in permissionConfigEntities)
                {
                    var permissionResult = workflowTemplatePermissionData.Add(item).GetAwaiter().GetResult().As<WorkflowTemplatePermissionDto>();
                    if (permissionResult == null)
                    {
                        throw new ArgumentException("添加工作流模板权限失败!");
                    }
                    if (result.PermissionConfig == null)
                    {
                        result.PermissionConfig = new List<WorkflowTemplatePermissionDto>();
                    }
                    result.PermissionConfig.Add(permissionResult);
                }
            }

            WorkflowTemplateFlowStepDesignEntity flowDesignEntity = requestDto.FlowDesign.As<WorkflowTemplateFlowStepDesignEntity>();
            flowDesignEntity.CreateAdminName = adminDto.FullName;
            flowDesignEntity.CreateAdminId = adminDto.AdminId;
            var stepDesignResult = this.workflowTemplateFlowStepDesignData.Add(flowDesignEntity).GetAwaiter().GetResult().As<WorkflowTemplateFlowStepDesignDto>();
            if (stepDesignResult == null)
            {
                throw new ArgumentException("添加工作流步骤配置总信息失败!");
            }
            stepDesignResult.FlowObjs = new List<WorkflowTemplateStepDto>();

            List<string> insertLineId = new List<string>();
            foreach (var item in requestDto.FlowDesign.FlowObjs)
            {
                WorkflowTemplateStepEntity stepEntity = item.As<WorkflowTemplateStepEntity>();
                stepEntity.CoordinateX = item.Coordinate[0];
                stepEntity.CoordinateY = item.Coordinate[1];
                stepEntity.TemplateCode = entity.TemplateCode;
                stepEntity.TemplateName = entity.TemplateName;
                var stepResult = this.workflowTemplateStepData.Add(stepEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepDto>();
                if (stepResult == null)
                {
                    throw new ArgumentException("添加工作流模板步骤信息失败!");
                }
                stepResult.Coordinate = new List<double>();
                stepResult.Coordinate.Add(stepEntity.CoordinateX);
                stepResult.Coordinate.Add(stepEntity.CoordinateY);
                stepResult.Lines = new List<WorkflowTemplateStepLineConfigDto>();
                stepResult.FormConfig = new List<object>();
                foreach (var line in item.Lines)
                {
                    var lineEntity = line.As<WorkflowTemplateStepLineConfigEntity>();
                    lineEntity.TemplateCode = entity.TemplateCode;
                    lineEntity.TemplateName = entity.TemplateName;
                    lineEntity.TemplateStepCode = stepResult.Id;
                    lineEntity.StepLineCode = lineEntity.Id;
                    lineEntity.StartCoordinateX = line.StartCoordinate[0];
                    lineEntity.StartCoordinateY = line.StartCoordinate[1];
                    lineEntity.EndCoordinateX = line.EndCoordinate[0];
                    lineEntity.EndCoordinateY = line.EndCoordinate[1];
                    lineEntity.PathsStr = JsonConvert.SerializeObject(line.Paths);
                    var lineDto = this.workflowTemplateStepLineConfigData.Add(lineEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepLineConfigDto>();
                    if (lineDto == null)
                    {
                        throw new ArgumentException("添加工作流步骤线条信息失败!");
                    }
                    lineDto.StartCoordinate = new List<double>();
                    lineDto.EndCoordinate = new List<double>();
                    lineDto.StartCoordinate.Add(line.StartCoordinate[0]);
                    lineDto.StartCoordinate.Add(line.StartCoordinate[1]);
                    lineDto.EndCoordinate.Add(line.EndCoordinate[0]);
                    lineDto.EndCoordinate.Add(line.EndCoordinate[1]);
                    lineDto.Paths = new List<List<double>>();
                    if (!insertLineId.Any(p => p.Equals(lineDto.StepLineCode)))
                    {
                        insertLineId.Add(lineDto.StepLineCode);
                        int pathOrder = 0;
                        foreach (var path in line.Paths)
                        {
                            WorkflowTemplateStepLinePathConfigEntity pathEntity = new WorkflowTemplateStepLinePathConfigEntity();
                            pathEntity.TemplateCode = entity.TemplateCode;
                            pathEntity.TemplateName = entity.TemplateName;
                            pathEntity.StepLineCode = lineDto.StepLineCode;
                            pathEntity.SvgLineId = lineDto.SvgLineId;
                            pathEntity.CoordinateX = path[0];
                            pathEntity.CoordinateY = path[1];
                            pathEntity.CoordinateOrder = pathOrder;
                            var linePathConfigResult = this.workflowTemplateStepLinePathConfigData.Add(pathEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepLinePathConfigDto>();
                            if (linePathConfigResult == null)
                            {
                                throw new ArgumentException("添加工作流步骤线条路径信息失败!");
                            }
                            lineDto.Paths.Add(path);
                            pathOrder++;
                        }
                    }
                    stepResult.Lines.Add(lineDto);
                }

                string jsonStr = JsonConvert.SerializeObject(item.FormConfig);
                JArray jarray = JArray.Parse(jsonStr);

                if (item.NodeType == OAStepNodeType.Start)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "StartBaseConfig", "基本配置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【基本配置】类型信息失败!");
                    }

                    WorkflowTemplateStepStartBaseConfigEntity workflowTemplateStepStartBaseConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepStartBaseConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepStartBaseConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepStartBaseConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepStartBaseConfigEntity.TemplateName = entity.TemplateName;

                    var startBaseConfigResult = this.workflowTemplateStepStartBaseConfigData.Add(workflowTemplateStepStartBaseConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepStartBaseConfigDto>();
                    if (startBaseConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤开始节点基本配置信息失败!");
                    }
                    stepResult.FormConfig.Add(startBaseConfigResult);

                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, jarray[1]["type"].ToString(), "表单配置", 1);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【表单配置】类型信息失败!");
                    }
                    List<WorkflowTemplateStepFormTemplateConfigEntity> workflowTemplateStepFormTemplateConfigEntities = JsonConvert.DeserializeObject<List<WorkflowTemplateStepFormTemplateConfigEntity>>(jarray[1]["data"].ToString(), this.JsonSerializerSettings);
                    List<WorkflowTemplateStepFormTemplateConfigDto> workflowTemplateStepFormTemplateConfigDtos = new List<WorkflowTemplateStepFormTemplateConfigDto>();
                    foreach (var formTemplateConfigEntity in workflowTemplateStepFormTemplateConfigEntities)
                    {
                        formTemplateConfigEntity.TemplateStepCode = stepResult.Id;
                        formTemplateConfigEntity.TemplateCode = entity.TemplateCode;
                        formTemplateConfigEntity.TemplateName = entity.TemplateName;
                        var formTemplateConfigResult = this.workflowTemplateStepFormTemplateConfigData.Add(formTemplateConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepFormTemplateConfigDto>();
                        if (formTemplateConfigResult == null)
                        {
                            throw new ArgumentException("添加工作流步骤模板表单信息失败!");
                        }
                        workflowTemplateStepFormTemplateConfigDtos.Add(formTemplateConfigResult);
                    }
                    stepResult.FormConfig.Add(new { Type = jarray[1]["type"].ToString(), Data = workflowTemplateStepFormTemplateConfigDtos });
                }
                else if (item.NodeType == OAStepNodeType.Node)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "NodeBaseConfig", "基本配置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【基本配置】类型信息失败!");
                    }

                    WorkflowTemplateStepBaseConfigEntity workflowTemplateStepBaseConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepBaseConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepBaseConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepBaseConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepBaseConfigEntity.TemplateName = entity.TemplateName;
                    var baseConfigResult = this.workflowTemplateStepBaseConfigData.Add(workflowTemplateStepBaseConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepBaseConfigDto>();
                    if (baseConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤节点基本配置信息失败!");
                    }
                    stepResult.FormConfig.Add(baseConfigResult);


                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, jarray[1]["type"].ToString(), "审核人员配置", 1);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【审核人员配置】类型信息失败!");
                    }
                    List<WorkflowTemplateStepReviewerConfigEntity> workflowTemplateStepReviewerConfigEntities = JsonConvert.DeserializeObject<List<WorkflowTemplateStepReviewerConfigEntity>>(jarray[1]["data"].ToString(), this.JsonSerializerSettings);
                    List<WorkflowTemplateStepReviewerConfigDto> workflowTemplateStepReviewerConfigDtos = new List<WorkflowTemplateStepReviewerConfigDto>();
                    foreach (var stepReviewerEntity in workflowTemplateStepReviewerConfigEntities)
                    {
                        stepReviewerEntity.TemplateStepCode = stepResult.Id;
                        stepReviewerEntity.TemplateCode = entity.TemplateCode;
                        stepReviewerEntity.TemplateName = entity.TemplateName;
                        var formTemplateConfigResult = this.workflowTemplateStepReviewerConfigData.Add(stepReviewerEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepReviewerConfigDto>();
                        if (formTemplateConfigResult == null)
                        {
                            throw new ArgumentException("添加工作流步骤模板审核人员信息失败!");
                        }
                        workflowTemplateStepReviewerConfigDtos.Add(formTemplateConfigResult);
                    }
                    stepResult.FormConfig.Add(new { Type = jarray[1]["type"].ToString(), Data = workflowTemplateStepReviewerConfigDtos });


                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, jarray[2]["type"].ToString(), "表单配置", 2);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【表单配置】类型信息失败!");
                    }
                    List<WorkflowTemplateStepFormTemplateConfigEntity> workflowTemplateStepFormTemplateConfigEntities = JsonConvert.DeserializeObject<List<WorkflowTemplateStepFormTemplateConfigEntity>>(jarray[2]["data"].ToString(), this.JsonSerializerSettings);
                    List<WorkflowTemplateStepFormTemplateConfigDto> workflowTemplateStepFormTemplateConfigDtos = new List<WorkflowTemplateStepFormTemplateConfigDto>();
                    foreach (var formTemplateConfigEntity in workflowTemplateStepFormTemplateConfigEntities)
                    {
                        formTemplateConfigEntity.TemplateStepCode = stepResult.Id;
                        formTemplateConfigEntity.TemplateCode = entity.TemplateCode;
                        formTemplateConfigEntity.TemplateName = entity.TemplateName;
                        var formTemplateConfigResult = this.workflowTemplateStepFormTemplateConfigData.Add(formTemplateConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepFormTemplateConfigDto>();
                        if (formTemplateConfigResult == null)
                        {
                            throw new ArgumentException("添加工作流步骤模板表单信息失败!");
                        }
                        workflowTemplateStepFormTemplateConfigDtos.Add(formTemplateConfigResult);
                    }
                    stepResult.FormConfig.Add(new { Type = jarray[2]["type"].ToString(), Data = workflowTemplateStepFormTemplateConfigDtos });


                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, jarray[3]["type"].ToString(), "按钮配置", 3);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【按钮配置】类型信息失败!");
                    }
                    List<WorkflowTemplateStepButtonConfigEntity> workflowTemplateStepButtonConfigEntities = JsonConvert.DeserializeObject<List<WorkflowTemplateStepButtonConfigEntity>>(jarray[3]["data"].ToString(), this.JsonSerializerSettings);
                    List<WorkflowTemplateStepButtonConfigDto> workflowTemplateStepButtonConfigDtos = new List<WorkflowTemplateStepButtonConfigDto>();
                    foreach (var stepButtonConfigEntity in workflowTemplateStepButtonConfigEntities)
                    {
                        stepButtonConfigEntity.TemplateStepCode = stepResult.Id;
                        stepButtonConfigEntity.TemplateCode = entity.TemplateCode;
                        stepButtonConfigEntity.TemplateName = entity.TemplateName;
                        var stepButtonConfigResult = this.workflowTemplateStepButtonConfigData.Add(stepButtonConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepButtonConfigDto>();
                        if (stepButtonConfigResult == null)
                        {
                            throw new ArgumentException("添加工作流步骤按钮配置信息失败!");
                        }
                        workflowTemplateStepButtonConfigDtos.Add(stepButtonConfigResult);
                    }
                    stepResult.FormConfig.Add(new { Type = jarray[3]["type"].ToString(), Data = workflowTemplateStepButtonConfigDtos });


                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "TimeoutSetting", "超时设置", 4);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【超时设置】类型信息失败!");
                    }

                    WorkflowTemplateStepNotificationConfigEntity workflowTemplateStepNotificationConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepNotificationConfigEntity>(jarray[4].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepNotificationConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepNotificationConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepNotificationConfigEntity.TemplateName = entity.TemplateName;
                    var notificationConfigDto = this.workflowTemplateStepNotificationConfigData.Add(workflowTemplateStepNotificationConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepNotificationConfigDto>();
                    if (notificationConfigDto == null)
                    {
                        throw new ArgumentException("添加步骤节点超时设置信息失败!");
                    }
                    stepResult.FormConfig.Add(notificationConfigDto);

                }
                else if (item.NodeType == OAStepNodeType.Countersign)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "CountersignSetting", "会签设置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【会签设置】类型信息失败!");
                    }
                    WorkflowTemplateStepCountersignConfigEntity workflowTemplateStepCountersignConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepCountersignConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepCountersignConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepCountersignConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepCountersignConfigEntity.TemplateName = entity.TemplateName;
                    var countersignConfigDto = this.workflowTemplateStepCountersignConfigData.Add(workflowTemplateStepCountersignConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepCountersignConfigDto>();
                    if (countersignConfigDto == null)
                    {
                        throw new ArgumentException("添加步骤节点超时设置信息失败!");
                    }
                    stepResult.FormConfig.Add(countersignConfigDto);
                }
                else if (item.NodeType == OAStepNodeType.Condition)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "ConditionBaseConfig", "条件基本配置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【条件基本配置】类型信息失败!");
                    }
                    WorkflowTemplateStepConditionBaseConfigEntity workflowTemplateStepConditionBaseConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepConditionBaseConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepConditionBaseConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepConditionBaseConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepConditionBaseConfigEntity.TemplateName = entity.TemplateName;
                    var conditionBaseConfigDto = this.workflowTemplateStepConditionBaseConfigData.Add(workflowTemplateStepConditionBaseConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepConditionBaseConfigDto>();
                    if (conditionBaseConfigDto == null)
                    {
                        throw new ArgumentException("添加步骤条件节点基本配置信息失败!");
                    }
                    stepResult.FormConfig.Add(conditionBaseConfigDto);


                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, jarray[1]["type"].ToString(), "表单条件配置", 1);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【表单条件配置】类型信息失败!");
                    }
                    List<WorkflowTemplateStepConditionConfigEntity> workflowTemplateStepConditionConfigEntities = JsonConvert.DeserializeObject<List<WorkflowTemplateStepConditionConfigEntity>>(jarray[1]["data"].ToString(), this.JsonSerializerSettings);
                    List<WorkflowTemplateStepConditionConfigDto> workflowTemplateStepConditionConfigDtos = new List<WorkflowTemplateStepConditionConfigDto>();
                    foreach (var conditionConfigEntity in workflowTemplateStepConditionConfigEntities)
                    {
                        conditionConfigEntity.TemplateStepCode = stepResult.Id;
                        conditionConfigEntity.TemplateCode = entity.TemplateCode;
                        conditionConfigEntity.TemplateName = entity.TemplateName;
                        var conditionConfigResult = this.workflowTemplateStepConditionConfigData.Add(conditionConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepConditionConfigDto>();
                        if (conditionConfigResult == null)
                        {
                            throw new ArgumentException("添加工作流步骤表单条件配置信息失败!");
                        }
                        workflowTemplateStepConditionConfigDtos.Add(conditionConfigResult);
                    }
                    stepResult.FormConfig.Add(new { Type = jarray[1]["type"].ToString(), Data = workflowTemplateStepConditionConfigDtos });

                }
                else if (item.NodeType == OAStepNodeType.SubFlow)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "SubFlowBaseConfig", "子流程基本配置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【子流程基本配置】类型信息失败!");
                    }
                    WorkflowTemplateStepSubFlowConfigEntity workflowTemplateStepSubFlowConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepSubFlowConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepSubFlowConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepSubFlowConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepSubFlowConfigEntity.TemplateName = entity.TemplateName;
                    var subFlowConfigDto = this.workflowTemplateStepSubFlowConfigData.Add(workflowTemplateStepSubFlowConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepSubFlowConfigDto>();
                    if (subFlowConfigDto == null)
                    {
                        throw new ArgumentException("添加步骤子流程节点基本配置信息失败!");
                    }
                    stepResult.FormConfig.Add(subFlowConfigDto);
                }
                else if (item.NodeType == OAStepNodeType.End)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "EndBaseConfig", "结束基本配置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【结束基本配置】类型信息失败!");
                    }
                    WorkflowTemplateStepEndBaseConfigEntity workflowTemplateStepEndBaseConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepEndBaseConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepEndBaseConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepEndBaseConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepEndBaseConfigEntity.TemplateName = entity.TemplateName;
                    var endBaseConfigDto = this.workflowTemplateStepEndBaseConfigData.Add(workflowTemplateStepEndBaseConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepEndBaseConfigDto>();
                    if (endBaseConfigDto == null)
                    {
                        throw new ArgumentException("添加步骤结束节点设置信息失败!");
                    }
                    stepResult.FormConfig.Add(endBaseConfigDto);
                }

                stepDesignResult.FlowObjs.Add(stepResult);
            }
            if (result.FlowDesign == null)
            {
                result.FlowDesign = new WorkflowTemplateFlowStepDesignDto();
            }
            result.FlowDesign = stepDesignResult;


            databaseContext.Commit();
            this.redisService.DeleteMultipleKey(string.Format("{0}Query",key), false);
            string cacheKey = this.redisService.GetKey(key, result.WorkflowTemplateId);
            this.redisService.AddAsync(cacheKey, result);
            return result;
        }
        #endregion

        #region 保存步骤配置信息类型
        /// <summary>
        /// 保存步骤配置信息类型
        /// </summary>
        /// <param name="entity">工作流模板信息</param>
        /// <param name="templateStepCode">工作流模板步骤Code</param>
        /// <param name="type">步骤配置类型</param>
        /// <param name="typeName">步骤配置类型名称</param>
        /// <param name="formOrder">步骤配置顺序</param>
        /// <returns></returns>
        private WorkflowTemplateStepTypeConfigDto SaveStepTypeConfig(WorkflowTemplateEntity entity, string templateStepCode, string type, string typeName, int formOrder)
        {
            WorkflowTemplateStepTypeConfigEntity stepTypeConfigEntity = new WorkflowTemplateStepTypeConfigEntity();
            stepTypeConfigEntity.TemplateCode = entity.TemplateCode;
            stepTypeConfigEntity.TemplateName = entity.TemplateName;
            stepTypeConfigEntity.Type = type;
            stepTypeConfigEntity.TypeName = typeName;
            stepTypeConfigEntity.FormOrder = formOrder;
            stepTypeConfigEntity.TemplateStepCode = templateStepCode;
            return this.workflowTemplateStepTypeConfigData.Add(stepTypeConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepTypeConfigDto>();
        }
        #endregion

        #region 修改工作流模板
        public WorkflowTemplateDto Update(WorkflowTemplatePutDto putDto,AdminDto adminDto)
        {
            if (!this.Exists(putDto.WorkflowTemplateId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<WorkflowTemplateEntity>();
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            List<WorkflowTemplatePermissionEntity> permissionConfigEntities = putDto.PermissionConfig.As<List<WorkflowTemplatePermissionEntity>>();
            databaseContext.BeginTransaction();

            var result = this.workflowTemplateData.Update(entity).GetAwaiter().GetResult().As<WorkflowTemplateDto>();
            if (result == null)
            {
                throw new ArgumentException("修改工作流模板信息失败!");
            }
            this.workflowTemplatePermissionData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateFlowStepDesignData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepTypeConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepLineConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepLinePathConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepStartBaseConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepFormTemplateConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepBaseConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepReviewerConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepButtonConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepNotificationConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepCountersignConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepConditionBaseConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepConditionConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepSubFlowConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepEndBaseConfigData.Delete(result.TemplateCode).GetAwaiter().GetResult();

            foreach (var item in permissionConfigEntities)
            {
                var permissionResult = this.workflowTemplatePermissionData.Add(item).GetAwaiter().GetResult().As<WorkflowTemplatePermissionDto>();
                if (permissionResult == null)
                {
                    throw new ArgumentException("添加工作流模板权限失败!");
                }
                if (result.PermissionConfig == null)
                {
                    result.PermissionConfig = new List<WorkflowTemplatePermissionDto>();
                }
                result.PermissionConfig.Add(permissionResult);
            }

            WorkflowTemplateFlowStepDesignEntity flowDesignEntity = putDto.FlowDesign.As<WorkflowTemplateFlowStepDesignEntity>();
            flowDesignEntity.CreateAdminName = adminDto.FullName;
            flowDesignEntity.CreateAdminId = adminDto.AdminId;
            var stepDesignResult = this.workflowTemplateFlowStepDesignData.Add(flowDesignEntity).GetAwaiter().GetResult().As<WorkflowTemplateFlowStepDesignDto>();
            if (stepDesignResult == null)
            {
                throw new ArgumentException("添加工作流步骤配置总信息失败!");
            }
            stepDesignResult.FlowObjs = new List<WorkflowTemplateStepDto>();
            List<string> insertLineId = new List<string>();
            foreach (var item in putDto.FlowDesign.FlowObjs)
            {
                WorkflowTemplateStepEntity stepEntity = item.As<WorkflowTemplateStepEntity>();
                stepEntity.CoordinateX = item.Coordinate[0];
                stepEntity.CoordinateY = item.Coordinate[1];
                stepEntity.TemplateCode = entity.TemplateCode;
                stepEntity.TemplateName = entity.TemplateName;
                var stepResult = this.workflowTemplateStepData.Add(stepEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepDto>();
                if (stepResult == null)
                {
                    throw new ArgumentException("添加工作流模板步骤信息失败!");
                }
                stepResult.Coordinate = new List<double>();
                stepResult.Coordinate.Add(stepEntity.CoordinateX);
                stepResult.Coordinate.Add(stepEntity.CoordinateY);
                stepResult.Lines = new List<WorkflowTemplateStepLineConfigDto>();
                stepResult.FormConfig = new List<object>();
                foreach (var line in item.Lines)
                {
                    var lineEntity = line.As<WorkflowTemplateStepLineConfigEntity>();
                    lineEntity.TemplateCode = entity.TemplateCode;
                    lineEntity.TemplateName = entity.TemplateName;
                    lineEntity.TemplateStepCode = stepResult.Id;
                    lineEntity.StepLineCode = lineEntity.Id;
                    lineEntity.StartCoordinateX = line.StartCoordinate[0];
                    lineEntity.StartCoordinateY = line.StartCoordinate[1];
                    lineEntity.EndCoordinateX = line.EndCoordinate[0];
                    lineEntity.EndCoordinateY = line.EndCoordinate[1];
                    lineEntity.PathsStr = JsonConvert.SerializeObject(line.Paths);
                    var lineDto = this.workflowTemplateStepLineConfigData.Add(lineEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepLineConfigDto>();
                    if (lineDto == null)
                    {
                        throw new ArgumentException("添加工作流步骤线条信息失败!");
                    }
                    lineDto.StartCoordinate = new List<double>();
                    lineDto.EndCoordinate = new List<double>();
                    lineDto.StartCoordinate.Add(line.StartCoordinate[0]);
                    lineDto.StartCoordinate.Add(line.StartCoordinate[1]);
                    lineDto.EndCoordinate.Add(line.EndCoordinate[0]);
                    lineDto.EndCoordinate.Add(line.EndCoordinate[1]);
                    lineDto.Paths = new List<List<double>>();
                    if (!insertLineId.Any(p => p.Equals(lineDto.StepLineCode)))
                    {
                        insertLineId.Add(lineDto.StepLineCode);
                        int pathOrder = 0;
                        foreach (var path in line.Paths)
                        {
                            WorkflowTemplateStepLinePathConfigEntity pathEntity = new WorkflowTemplateStepLinePathConfigEntity();
                            pathEntity.TemplateCode = entity.TemplateCode;
                            pathEntity.TemplateName = entity.TemplateName;
                            pathEntity.StepLineCode = lineDto.StepLineCode;
                            pathEntity.SvgLineId = lineDto.SvgLineId;
                            pathEntity.CoordinateX = path[0];
                            pathEntity.CoordinateY = path[1];
                            pathEntity.CoordinateOrder = pathOrder;
                            var linePathConfigResult = this.workflowTemplateStepLinePathConfigData.Add(pathEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepLinePathConfigDto>();
                            if (linePathConfigResult == null)
                            {
                                throw new ArgumentException("添加工作流步骤线条路径信息失败!");
                            }
                            lineDto.Paths.Add(path);
                            pathOrder++;
                        }
                    }
                    stepResult.Lines.Add(lineDto);
                }

                string jsonStr = JsonConvert.SerializeObject(item.FormConfig);
                JArray jarray = JArray.Parse(jsonStr);

                if (item.NodeType == OAStepNodeType.Start)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "StartBaseConfig", "基本配置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【基本配置】类型信息失败!");
                    }

                    WorkflowTemplateStepStartBaseConfigEntity workflowTemplateStepStartBaseConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepStartBaseConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepStartBaseConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepStartBaseConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepStartBaseConfigEntity.TemplateName = entity.TemplateName;
                    var startBaseConfigResult = this.workflowTemplateStepStartBaseConfigData.Add(workflowTemplateStepStartBaseConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepStartBaseConfigDto>();
                    if (startBaseConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤开始节点基本配置信息失败!");
                    }
                    stepResult.FormConfig.Add(startBaseConfigResult);

                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, jarray[1]["type"].ToString(), "表单配置", 1);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【表单配置】类型信息失败!");
                    }
                    List<WorkflowTemplateStepFormTemplateConfigEntity> workflowTemplateStepFormTemplateConfigEntities = JsonConvert.DeserializeObject<List<WorkflowTemplateStepFormTemplateConfigEntity>>(jarray[1]["data"].ToString(), this.JsonSerializerSettings);
                    List<WorkflowTemplateStepFormTemplateConfigDto> workflowTemplateStepFormTemplateConfigDtos = new List<WorkflowTemplateStepFormTemplateConfigDto>();
                    foreach (var formTemplateConfigEntity in workflowTemplateStepFormTemplateConfigEntities)
                    {
                        formTemplateConfigEntity.TemplateStepCode = stepResult.Id;
                        formTemplateConfigEntity.TemplateCode = entity.TemplateCode;
                        formTemplateConfigEntity.TemplateName = entity.TemplateName;
                        var formTemplateConfigResult = this.workflowTemplateStepFormTemplateConfigData.Add(formTemplateConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepFormTemplateConfigDto>();
                        if (formTemplateConfigResult == null)
                        {
                            throw new ArgumentException("添加工作流步骤模板表单信息失败!");
                        }
                        workflowTemplateStepFormTemplateConfigDtos.Add(formTemplateConfigResult);
                    }
                    stepResult.FormConfig.Add(new { Type = jarray[1]["type"].ToString(), Data = workflowTemplateStepFormTemplateConfigDtos });
                }
                else if (item.NodeType == OAStepNodeType.Node)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "NodeBaseConfig", "基本配置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【基本配置】类型信息失败!");
                    }
                   
                    WorkflowTemplateStepBaseConfigEntity workflowTemplateStepBaseConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepBaseConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepBaseConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepBaseConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepBaseConfigEntity.TemplateName = entity.TemplateName;
                    var baseConfigResult = this.workflowTemplateStepBaseConfigData.Add(workflowTemplateStepBaseConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepBaseConfigDto>();
                    if (baseConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤节点基本配置信息失败!");
                    }
                    stepResult.FormConfig.Add(baseConfigResult);


                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, jarray[1]["type"].ToString(), "审核人员配置", 1);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【审核人员配置】类型信息失败!");
                    }
                    List<WorkflowTemplateStepReviewerConfigEntity> workflowTemplateStepReviewerConfigEntities = JsonConvert.DeserializeObject<List<WorkflowTemplateStepReviewerConfigEntity>>(jarray[1]["data"].ToString(), this.JsonSerializerSettings);
                    List<WorkflowTemplateStepReviewerConfigDto> workflowTemplateStepReviewerConfigDtos = new List<WorkflowTemplateStepReviewerConfigDto>();
                    foreach (var stepReviewerEntity in workflowTemplateStepReviewerConfigEntities)
                    {
                        stepReviewerEntity.TemplateStepCode = stepResult.Id;
                        stepReviewerEntity.TemplateCode = entity.TemplateCode;
                        stepReviewerEntity.TemplateName = entity.TemplateName;
                        var formTemplateConfigResult = this.workflowTemplateStepReviewerConfigData.Add(stepReviewerEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepReviewerConfigDto>();
                        if (formTemplateConfigResult == null)
                        {
                            throw new ArgumentException("添加工作流步骤模板审核人员信息失败!");
                        }
                        workflowTemplateStepReviewerConfigDtos.Add(formTemplateConfigResult);
                    }
                    stepResult.FormConfig.Add(new { Type = jarray[1]["type"].ToString(), Data = workflowTemplateStepReviewerConfigDtos });


                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, jarray[2]["type"].ToString(), "表单配置", 2);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【表单配置】类型信息失败!");
                    }
                    List<WorkflowTemplateStepFormTemplateConfigEntity> workflowTemplateStepFormTemplateConfigEntities = JsonConvert.DeserializeObject<List<WorkflowTemplateStepFormTemplateConfigEntity>>(jarray[2]["data"].ToString(), this.JsonSerializerSettings);
                    List<WorkflowTemplateStepFormTemplateConfigDto> workflowTemplateStepFormTemplateConfigDtos = new List<WorkflowTemplateStepFormTemplateConfigDto>();
                    foreach (var formTemplateConfigEntity in workflowTemplateStepFormTemplateConfigEntities)
                    {
                        formTemplateConfigEntity.TemplateStepCode = stepResult.Id;
                        formTemplateConfigEntity.TemplateCode = entity.TemplateCode;
                        formTemplateConfigEntity.TemplateName = entity.TemplateName;
                        var formTemplateConfigResult = this.workflowTemplateStepFormTemplateConfigData.Add(formTemplateConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepFormTemplateConfigDto>();
                        if (formTemplateConfigResult == null)
                        {
                            throw new ArgumentException("添加工作流步骤模板表单信息失败!");
                        }
                        workflowTemplateStepFormTemplateConfigDtos.Add(formTemplateConfigResult);
                    }
                    stepResult.FormConfig.Add(new { Type = jarray[2]["type"].ToString(), Data = workflowTemplateStepFormTemplateConfigDtos });


                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, jarray[3]["type"].ToString(), "按钮配置", 3);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【按钮配置】类型信息失败!");
                    }
                    List<WorkflowTemplateStepButtonConfigEntity> workflowTemplateStepButtonConfigEntities = JsonConvert.DeserializeObject<List<WorkflowTemplateStepButtonConfigEntity>>(jarray[3]["data"].ToString(), this.JsonSerializerSettings);
                    List<WorkflowTemplateStepButtonConfigDto> workflowTemplateStepButtonConfigDtos = new List<WorkflowTemplateStepButtonConfigDto>();
                    foreach (var stepButtonConfigEntity in workflowTemplateStepButtonConfigEntities)
                    {
                        stepButtonConfigEntity.TemplateStepCode = stepResult.Id;
                        stepButtonConfigEntity.TemplateCode = entity.TemplateCode;
                        stepButtonConfigEntity.TemplateName = entity.TemplateName;
                        var stepButtonConfigResult = this.workflowTemplateStepButtonConfigData.Add(stepButtonConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepButtonConfigDto>();
                        if (stepButtonConfigResult == null)
                        {
                            throw new ArgumentException("添加工作流步骤按钮配置信息失败!");
                        }
                        workflowTemplateStepButtonConfigDtos.Add(stepButtonConfigResult);
                    }
                    stepResult.FormConfig.Add(new { Type = jarray[3]["type"].ToString(), Data = workflowTemplateStepButtonConfigDtos });


                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "TimeoutSetting", "超时设置", 4);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【超时设置】类型信息失败!");
                    }

                    WorkflowTemplateStepNotificationConfigEntity workflowTemplateStepNotificationConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepNotificationConfigEntity>(jarray[4].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepNotificationConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepNotificationConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepNotificationConfigEntity.TemplateName = entity.TemplateName;
                    var notificationConfigDto = this.workflowTemplateStepNotificationConfigData.Add(workflowTemplateStepNotificationConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepNotificationConfigDto>();
                    if (notificationConfigDto == null)
                    {
                        throw new ArgumentException("添加步骤节点超时设置信息失败!");
                    }
                    stepResult.FormConfig.Add(notificationConfigDto);

                }
                else if (item.NodeType == OAStepNodeType.Countersign)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "CountersignSetting", "会签设置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【会签设置】类型信息失败!");
                    }
                    WorkflowTemplateStepCountersignConfigEntity workflowTemplateStepCountersignConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepCountersignConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepCountersignConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepCountersignConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepCountersignConfigEntity.TemplateName = entity.TemplateName;
                    var countersignConfigDto = this.workflowTemplateStepCountersignConfigData.Add(workflowTemplateStepCountersignConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepCountersignConfigDto>();
                    if (countersignConfigDto == null)
                    {
                        throw new ArgumentException("添加步骤节点超时设置信息失败!");
                    }
                    stepResult.FormConfig.Add(countersignConfigDto);
                }
                else if (item.NodeType == OAStepNodeType.Condition)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "ConditionBaseConfig", "条件基本配置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【条件基本配置】类型信息失败!");
                    }
                    WorkflowTemplateStepConditionBaseConfigEntity workflowTemplateStepConditionBaseConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepConditionBaseConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepConditionBaseConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepConditionBaseConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepConditionBaseConfigEntity.TemplateName = entity.TemplateName;
                    var conditionBaseConfigDto = this.workflowTemplateStepConditionBaseConfigData.Add(workflowTemplateStepConditionBaseConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepConditionBaseConfigDto>();
                    if (conditionBaseConfigDto == null)
                    {
                        throw new ArgumentException("添加步骤条件节点基本配置信息失败!");
                    }
                    stepResult.FormConfig.Add(conditionBaseConfigDto);


                    stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, jarray[1]["type"].ToString(), "表单条件配置", 1);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【表单条件配置】类型信息失败!");
                    }
                    List<WorkflowTemplateStepConditionConfigEntity> workflowTemplateStepConditionConfigEntities = JsonConvert.DeserializeObject<List<WorkflowTemplateStepConditionConfigEntity>>(jarray[1]["data"].ToString(), this.JsonSerializerSettings);
                    List<WorkflowTemplateStepConditionConfigDto> workflowTemplateStepConditionConfigDtos = new List<WorkflowTemplateStepConditionConfigDto>();
                    foreach (var conditionConfigEntity in workflowTemplateStepConditionConfigEntities)
                    {
                        conditionConfigEntity.TemplateStepCode = stepResult.Id;
                        conditionConfigEntity.TemplateCode = entity.TemplateCode;
                        conditionConfigEntity.TemplateName = entity.TemplateName;
                        var conditionConfigResult = this.workflowTemplateStepConditionConfigData.Add(conditionConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepConditionConfigDto>();
                        if (conditionConfigResult == null)
                        {
                            throw new ArgumentException("添加工作流步骤表单条件配置信息失败!");
                        }
                        workflowTemplateStepConditionConfigDtos.Add(conditionConfigResult);
                    }
                    stepResult.FormConfig.Add(new { Type = jarray[1]["type"].ToString(), Data = workflowTemplateStepConditionConfigDtos });

                }
                else if (item.NodeType == OAStepNodeType.SubFlow)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "SubFlowBaseConfig", "子流程基本配置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【子流程基本配置】类型信息失败!");
                    }
                    WorkflowTemplateStepSubFlowConfigEntity workflowTemplateStepSubFlowConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepSubFlowConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepSubFlowConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepSubFlowConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepSubFlowConfigEntity.TemplateName = entity.TemplateName;
                    var subFlowConfigDto = this.workflowTemplateStepSubFlowConfigData.Add(workflowTemplateStepSubFlowConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepSubFlowConfigDto>();
                    if (subFlowConfigDto == null)
                    {
                        throw new ArgumentException("添加步骤子流程节点基本配置信息失败!");
                    }
                    stepResult.FormConfig.Add(subFlowConfigDto);
                }
                else if (item.NodeType == OAStepNodeType.End)
                {
                    var stepTypeConfigResult = this.SaveStepTypeConfig(entity, stepResult.Id, "EndBaseConfig", "结束基本配置", 0);
                    if (stepTypeConfigResult == null)
                    {
                        throw new ArgumentException("添加步骤配置【结束基本配置】类型信息失败!");
                    }
                    WorkflowTemplateStepEndBaseConfigEntity workflowTemplateStepEndBaseConfigEntity = JsonConvert.DeserializeObject<WorkflowTemplateStepEndBaseConfigEntity>(jarray[0].ToString(), this.JsonSerializerSettings);
                    workflowTemplateStepEndBaseConfigEntity.TemplateStepCode = stepResult.Id;
                    workflowTemplateStepEndBaseConfigEntity.TemplateCode = entity.TemplateCode;
                    workflowTemplateStepEndBaseConfigEntity.TemplateName = entity.TemplateName;
                    var endBaseConfigDto = this.workflowTemplateStepEndBaseConfigData.Add(workflowTemplateStepEndBaseConfigEntity).GetAwaiter().GetResult().As<WorkflowTemplateStepEndBaseConfigDto>();
                    if (endBaseConfigDto == null)
                    {
                        throw new ArgumentException("添加步骤结束节点设置信息失败!");
                    }
                    stepResult.FormConfig.Add(endBaseConfigDto);
                }

                stepDesignResult.FlowObjs.Add(stepResult);
            }
            if (result.FlowDesign == null)
            {
                result.FlowDesign = new WorkflowTemplateFlowStepDesignDto();
            }
            result.FlowDesign = stepDesignResult;

            databaseContext.Commit();
            
            this.redisService.DeleteMultipleKey(string.Format("{0}Query", key), false);
            string cacheKey = this.redisService.GetKey(key, result.WorkflowTemplateId);
            this.redisService.DeleteAsync(cacheKey);
            this.redisService.AddAsync(cacheKey, result);
            return result;
        }
        #endregion

        #region 删除工作流模板
        public bool Delete(long workflowTemplateId)
        {
            if (!this.Exists(workflowTemplateId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = this.workflowTemplateData.GetEntity(workflowTemplateId).Result;
            databaseContext.BeginTransaction();
            this.workflowTemplatePermissionData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateFlowStepDesignData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepTypeConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepLineConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepLinePathConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepStartBaseConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepFormTemplateConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepBaseConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepReviewerConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepButtonConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepNotificationConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepCountersignConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepConditionBaseConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepConditionConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepSubFlowConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            this.workflowTemplateStepEndBaseConfigData.Delete(entity.TemplateCode).GetAwaiter().GetResult();
            var result = this.workflowTemplateData.Delete(workflowTemplateId).GetAwaiter().GetResult();
            databaseContext.Commit();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 获取工作流模板
        public WorkflowTemplateDto GetBySpecificKey(string specificTemplateKey)
        {
            var workflowTemplateEntity = this.workflowTemplateData.GetBySpecificKey(specificTemplateKey).GetAwaiter().GetResult();
            if (workflowTemplateEntity == null)
            {
                throw new ArgumentException("此工作流模板不存在!");
            }
            return this.Get(workflowTemplateEntity.WorkflowTemplateId);
        }
        public WorkflowTemplateDto Get(string workflowTemplateCode)
        {
            var workflowTemplateEntity = this.workflowTemplateData.GetEntity(workflowTemplateCode).GetAwaiter().GetResult();
            if (workflowTemplateEntity == null)
            {
                throw new ArgumentException("此工作流模板不存在!");
            }
            return this.Get(workflowTemplateEntity.WorkflowTemplateId);
        }
        public WorkflowTemplateDto Get(long workflowTemplateId)
        {
            string cacheKey = this.redisService.GetKey(key, workflowTemplateId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.workflowTemplateData.GetEntity(workflowTemplateId).GetAwaiter().GetResult().As<WorkflowTemplateDto>();
                if (data != null)
                {
                    var permissionList = this.workflowTemplatePermissionData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplatePermissionDto>>(); ;
                    var stepDesignData = this.workflowTemplateFlowStepDesignData.GetEntity(data.TemplateCode).Result.As<WorkflowTemplateFlowStepDesignDto>();
                    var stepList = this.workflowTemplateStepData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepDto>>();
                    var lineConfigList = this.workflowTemplateStepLineConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepLineConfigDto>>();
                    var linePathConfigList = this.workflowTemplateStepLinePathConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepLinePathConfigDto>>();
                    var typeConfigList = this.workflowTemplateStepTypeConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepTypeConfigDto>>();
                    var startBaseConfigList = this.workflowTemplateStepStartBaseConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepStartBaseConfigDto>>();
                    var formTemplateConfigList = this.workflowTemplateStepFormTemplateConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepFormTemplateConfigDto>>();
                    var baseConfigList = this.workflowTemplateStepBaseConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepBaseConfigDto>>();
                    var reviewerConfigList = this.workflowTemplateStepReviewerConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepReviewerConfigDto>>();
                    var buttonConfigList = this.workflowTemplateStepButtonConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepButtonConfigDto>>();

                    var notificationConfigList = this.workflowTemplateStepNotificationConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepNotificationConfigDto>>();
                    var countersignConfigList = this.workflowTemplateStepCountersignConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepCountersignConfigDto>>();
                    var conditionBaseConfigList = this.workflowTemplateStepConditionBaseConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepConditionBaseConfigDto>>();
                    var conditionConfigList = this.workflowTemplateStepConditionConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepConditionConfigDto>>();
                    var subflowConfigList = this.workflowTemplateStepSubFlowConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepSubFlowConfigDto>>();
                    var endBaseConfigList = this.workflowTemplateStepEndBaseConfigData.GetList(data.TemplateCode).Result.As<List<WorkflowTemplateStepEndBaseConfigDto>>();

                    data.PermissionConfig = permissionList;

                    foreach (var step in stepList)
                    {
                        step.Coordinate.Add(step.CoordinateX);
                        step.Coordinate.Add(step.CoordinateY);
                        var stepLines = lineConfigList.Where(p => p.TemplateStepCode == step.Id);
                        foreach (var stepLine in stepLines)
                        {
                            var linePathList = linePathConfigList.Where(p => p.SvgLineId == stepLine.SvgLineId);
                            List<List<double>> paths = new List<List<double>>();
                            foreach (var path in linePathList)
                            {
                                paths.Add(new List<double> { path.CoordinateX, path.CoordinateY });
                            }
                            stepLine.Paths = paths;
                            stepLine.StartCoordinate = new List<double> { stepLine.StartCoordinateX, stepLine.StartCoordinateY };
                            stepLine.EndCoordinate = new List<double> { stepLine.EndCoordinateX, stepLine.EndCoordinateY };
                        }
                        step.Lines = stepLines.ToList();
                        List<object> formConfig = new List<object>();

                        foreach (var typeConfig in typeConfigList.Where(p => p.TemplateStepCode == step.Id).OrderBy(p => p.FormOrder))
                        {
                            OAStepNodeFormType type = (OAStepNodeFormType)Enum.Parse(typeof(OAStepNodeFormType), typeConfig.Type);
                            if (type == OAStepNodeFormType.StartBaseConfig)
                            {
                                var entity = startBaseConfigList.Single(p => p.TemplateStepCode == step.Id);
                                Dictionary<string,object> dic = ClassToMap.ToMap(entity);
                                dic.Add("Type", type.ToString());
                                formConfig.Add((JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dic, this.ResultDictionaryJsonSerializerSettings)));
                            }
                            else if (type == OAStepNodeFormType.FormTemplateSetting)
                            {
                                formConfig.Add(new { Type = typeConfig.Type, Data = formTemplateConfigList.Where(p => p.TemplateStepCode == step.Id) });
                            }
                            else if (type == OAStepNodeFormType.NodeBaseConfig)
                            {
                                var entity = baseConfigList.Single(p => p.TemplateStepCode == step.Id);
                                Dictionary<string, object> dic = ClassToMap.ToMap(entity);
                                dic.Add("Type", type.ToString());
                                formConfig.Add((JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dic, this.ResultDictionaryJsonSerializerSettings)));
                            }
                            else if (type == OAStepNodeFormType.ReviewerSetting)
                            {
                                formConfig.Add(new { Type = typeConfig.Type, Data = reviewerConfigList.Where(p => p.TemplateStepCode == step.Id) });
                            }
                            else if (type == OAStepNodeFormType.ButtonSetting)
                            {
                                formConfig.Add(new { Type = typeConfig.Type, Data = buttonConfigList.Where(p => p.TemplateStepCode == step.Id) });
                            }
                            else if (type == OAStepNodeFormType.TimeoutSetting)
                            {
                                var entity = notificationConfigList.Single(p => p.TemplateStepCode == step.Id);
                                Dictionary<string, object> dic = ClassToMap.ToMap(entity);
                                dic.Add("Type", type.ToString());
                                formConfig.Add((JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dic, this.ResultDictionaryJsonSerializerSettings)));
                            }
                            else if (type == OAStepNodeFormType.CountersignSetting)
                            {
                                var entity = countersignConfigList.Single(p => p.TemplateStepCode == step.Id);
                                Dictionary<string, object> dic = ClassToMap.ToMap(entity);
                                dic.Add("Type", type.ToString());
                                formConfig.Add((JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dic, this.ResultDictionaryJsonSerializerSettings)));
                            }
                            else if (type == OAStepNodeFormType.ConditionBaseConfig)
                            {
                                var entity = conditionBaseConfigList.Single(p => p.TemplateStepCode == step.Id);
                                Dictionary<string, object> dic = ClassToMap.ToMap(entity);
                                dic.Add("Type", type.ToString());
                                formConfig.Add((JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dic, this.ResultDictionaryJsonSerializerSettings)));
                            }
                            else if (type == OAStepNodeFormType.TemplateFormCondition)
                            {
                                formConfig.Add(new { Type = typeConfig.Type, Data = conditionConfigList.Where(p => p.TemplateStepCode == step.Id) });
                            }
                            else if (type == OAStepNodeFormType.SubFlowBaseConfig)
                            {
                                var entity = subflowConfigList.Single(p => p.TemplateStepCode == step.Id);
                                Dictionary<string, object> dic = ClassToMap.ToMap(entity);
                                dic.Add("Type", type.ToString());
                                formConfig.Add((JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dic, this.ResultDictionaryJsonSerializerSettings)));
                            }
                            else
                            {
                                var entity = endBaseConfigList.Single(p => p.TemplateStepCode == step.Id);
                                Dictionary<string, object> dic = ClassToMap.ToMap(entity);
                                dic.Add("Type", type.ToString());
                                formConfig.Add((JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dic, this.ResultDictionaryJsonSerializerSettings)));
                            }
                        }
                        step.FormConfig = formConfig;
                    }

                    stepDesignData.FlowObjs = stepList;
                    data.FlowDesign = stepDesignData;
                    return data;
                }
                return data;
            });
            if (result == null)
            {
                this.redisService.DeleteAsync(cacheKey);
            }
            return result;
        }
        #endregion

        #region 获取工作流模板列表
        public IEnumerable<WorkflowTemplateDto> GetList(string category_code)
        {
            string cacheKey = this.redisService.GetKey(string.Format("{0}Query", key), category_code);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.workflowTemplateData.GetList(category_code).GetAwaiter().GetResult();
            }, TimeSpan.FromDays(1));
            if (result == null)
            {
                this.redisService.DeleteAsync(cacheKey);
            }
            return result.As<IEnumerable<WorkflowTemplateDto>>();
        }
        #endregion

        #region 获取工作流模板分页
        public PageableList<WorkflowTemplateDto> Query(string categoryCode, string templateName, bool? isBaseFlow, bool? isMobleShow, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(string.Format("{0}Query", key), categoryCode, templateName, isBaseFlow, isMobleShow, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.workflowTemplateData.Query(categoryCode, templateName, isBaseFlow, isMobleShow, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<WorkflowTemplateDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<WorkflowTemplateDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            });
            if (result == null)
            {
                this.redisService.DeleteAsync(cacheKey);
            }
            return result;
        }
        #endregion

        #region 获取允许我发起的工作流列表
        public IEnumerable<WorkflowTemplateDto> GetAllowInitiateList(AdminDto adminDto)
        {
            string cacheKey = string.Format("{0}AllowInitiateList:CompanyId={1}:DepartmentId={2}:AdminId={3}:PositionId={4}", key, adminDto.CompanyId, adminDto.DepartmentId, adminDto.AdminId, adminDto.PositionId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.workflowTemplateData.GetAllowInitiateList(adminDto.CompanyId, adminDto.DepartmentId, adminDto.AdminId, adminDto.PositionId).GetAwaiter().GetResult();
            }, TimeSpan.FromDays(1));
            if (result == null)
            {
                this.redisService.DeleteAsync(cacheKey);
            }
            return result.As<IEnumerable<WorkflowTemplateDto>>();
        }
        #endregion

        #region 获取工作流模板条件步骤配置
        public IEnumerable<WorkflowTemplateStepConditionConfigAllDto> GetWorkflowTemplateStepConditionConfigs(string templateCode)
        {
            return this.workflowTemplateStepConditionConfigData.GetList(templateCode).GetAwaiter().GetResult().As<IEnumerable<WorkflowTemplateStepConditionConfigAllDto>>();
        }
        #endregion
    }
}